#!/bin/bash
# BashChat by BiteTheByte [bitethebyte (at) gmail (dot) com]
# Licensed under GPLv3
# This program is written in bash scripting and comes with absolutely NO WARRANTY
# Be carefull and use it with "caution and wisdom".
# :) Enjoy ssh chatting!!!

create=""
room=""
search=""
escape='-q'
wookudir="/tmp"
prefix="clerking"
separator="_"
input=""
nickname=""
printdate="" #Post publishing date
lastdate="" #Last post publishing date
declare -i nick_len=0;
declare -i max_nick_len=26;
declare -i mesg_len=0;
declare -i max_mesg_len=256;
declare -i max_room_len=50;
#I don't know if do-whiles exists so, while-do FTW!
while [ -z "$create" ]; do
#Menu
echo "";
echo "1) Join a chatroom";
echo "2) Create a new chatroom";
echo "$escape) Quit"
#echo " -> : "; 
read create
#If there is no such an option, get back to the menu.
if [ "$create" != "1" ] && [ "$create" != "2" ] && [ "$create" != "$escape" ];
 then
	create=""
 fi
done
#Checking exit condition
if [ "$create" == "$escape" ]; then
exit 0
fi
#Creating chatroom - Option
if [ "$create" == "2" ];
 then
	while [ -z "$room" ]; do
	echo "Type the new chatroom's name (spaces and '$separator' characters are not allowed)"
	echo "$escape) to quit                                      ($max_room_len characters max)"
	read room
	#Checkig exit condition
	if [ "$room" == "$escape" ]; then
		exit 1
	fi
	#Checking for empty room name, already existing one or too long. (Adjustable)
	if [ -z "$room" ] || [ -e "$wookudir/$prefix$separator$room" ] || [ $(echo "$room" | wc -w) != "1" ] || [ -n "$(echo "$room" | grep "$separator")" ] || (( $(echo "$room" | wc -m) > $max_room_len )); then
		room="";
		echo ""
		echo "ERROR: Chatroom name already in use (or not valid)"
		echo ""
		
	fi
	done
	#History creation
	touch "$wookudir/$prefix$separator$room"
	chmod 644 "$wookudir/$prefix$separator$room"
	#PipeLine creation
	mknod "$wookudir/$prefix$separator$room$separator""p" "p"
	chmod 622 "$wookudir/$prefix$separator$room$separator""p"
	#Banner insertion
	echo -e " \t#########################################" >> "$wookudir/$prefix$separator$room"
	echo -e " \t# ChatRoom sponsored by:                #" >> "$wookudir/$prefix$separator$room"
	echo -e " \t#       ||EXAMINATOR 3000|| (GPLv3)     #" >> "$wookudir/$prefix$separator$room"
	echo -e " \t#########################################" >> "$wookudir/$prefix$separator$room"
	echo "" >> "$wookudir/$prefix$separator$room"
	echo "" >> "$wookudir/$prefix$separator$room"
#Piper daemon creation
	./chat_pipe "$wookudir/$prefix$separator$room" "$separator" "$max_mesg_len" &
else
#Room search
	while [ -z "$room" ]; do
	search=$(find "$wookudir" -type p -name "$prefix$separator*$separator""p" -perm 622 2> /dev/null | cut -d "$separator" -f 2)
	#Cutie Menu
	echo "Select an available chatroom"
	echo ""
	search=$search$'\n'"Quit"
	select room in $search
	 do
		#Exit Option
		if [ "$room" == "Quit" ]; then
			exit 1
		fi
		#Checking Non-Existing File
		if [ -z "$room" ] || [ ! -e "$wookudir/$prefix$separator$room" ]; then
        	        echo ""
               	 echo "Chatroom not exists"
               	 echo ""
               	 echo ""
		 room=""
		else
			break
		fi
	 done
	done
 fi
#Looping Nickname Request
while [ -z "$nickname" ]; do
echo "Type Nickname ($escape to quit)" 
read nickname
#Checking Exit Option
if [ "$nickname" == "$escape" ]; then
	#Proper Quitting Actions
	if [ "$create" == "2" ]; then
	  chmod 600 "$wookudir/$prefix$separator$room$separator""p";
          pkill chat_pipe & ./chat_post "$wookudir/$prefix$separator$room$separator""p" "####### ROOM CLOSED BY OP #######" "$escape"; 
          ./chat_post "$wookudir/$prefix$separator$room" "####### ROOM CLOSED BY OP #######" "$escape";
          rm "$wookudir/$prefix$separator$room" "$wookudir/$prefix$separator$room$separator""p"
	fi
 exit 0
fi
nick_len="$(echo "$nickname" | wc -m)";
if (( $nick_len >  $max_nick_len )); then
	echo ""
	echo -e "ERROR: Nickname too long! (Max $max_nick_len)"
	nickname="";
	echo ""
fi
done
#'Getting into the room' Message.
./chat_post "$wookudir/$prefix$separator$room$separator""p" "++   [$(date +%H:%M)] $nickname just entered the chatroom" "$escape" &


#chat_watch-daemon start.
./chat_watch "$wookudir" "$prefix$separator$room" "$escape" "$nickname"&
#Posting Loop
while [ "$input" != "$escape" ]; do
read -e input
date=$(date +%H:%M)
#Checking for Empty Message or Non-Quitting escape string.
if [ -n "$input" ] && [ "$input" != "$escape" ]; then
	mesg_len="$(echo "$input" | wc -m)";
	if (( $mesg_len > $max_mesg_len )); then
		continue;
	fi
	#Posting-Date Update condition
	if [ "$lastdate" == "$date" ]; then
		printdate="       "
	else
		printdate="[$date]"
	fi
	./chat_post "$wookudir/$prefix$separator$room$separator""p" "$printdate <$nickname> : $input" "$escape" &
	lastdate=$(date +%H:%M) #Last post date post-update
fi
done
#Room exit
./chat_post "$wookudir/$prefix$separator$room$separator""p" "--   $nickname just left the room" "$escape" &
pkill chat_watch;
if [ "$create" == "2" ]; then
	chmod 600 "$wookudir/$prefix$separator$room$separator""p";
        pkill chat_pipe;
        ./chat_post "$wookudir/$prefix$separator$room$separator""p" "####### ROOM CLOSED BY OP #######" "$escape" &
	./chat_post "$wookudir/$prefix$separator$room" "####### ROOM CLOSED BY OP #######" "$escape" &
	sleep 1;
	pkill chat_post;
        rm "$wookudir/$prefix$separator$room" "$wookudir/$prefix$separator$room$separator""p"
fi
pkill chat_post;
clear
